package cn.e4j.modular.api.controller;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.util.LangUtils;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.service.GwaService;
import cn.e4j.modular.api.service.Gwat900Service;
import cn.e4j.modular.api.service.GwbService;
import cn.e4j.modular.api.util.CommonUtils;
import cn.e4j.modular.crm.entity.GwakT;
import cn.e4j.modular.crm.entity.GwaklT;
import cn.e4j.modular.crm.entity.GwalT;
import cn.e4j.modular.system.entity.SysUser;
import cn.e4j.modular.system.service.impl.SystemServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户信息修改 控制类
 * Created by shixc on 2019/4/24.
 */
@RestController
@RequestMapping("api/wx/gwat900")
public class Gwat900Controller {
    private static Logger logger = LoggerFactory.getLogger(Gwat900Controller.class);

    @Autowired
    Gwat900Service gwat900Service;

    @Autowired
    SystemServiceImpl systemService;

    @Autowired
    GwaService gwaService;

    @Autowired
    GwbService gwbService;

    /**
     * 获取客户列表
     *
     * @return
     */
    @RequestMapping(value = "getGwakList")
    public ResponseData getGwakList(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser user = wapper.getSysUser();
        inDto.put("GWAKENT", inDto.getString("ENT"));
        inDto.put("GWAKACTI", "Y");
        List<Dto> list = new ArrayList<>();
        try {
            //客户权限
            inDto.put("GWAK006", "1");
            Dto gwb001 = gwbService.getGwb001(user,inDto);
            List<Dto> _list = (List<Dto>) gwb001.get("CUSTOMERS");
            inDto.put("CUSTOMERS", _list);

            //经销商权限
            inDto.put("GWAK006", "2");
            Dto gwb001_1 = gwbService.getGwb001(user,inDto);
            List<Dto> _list1 = (List<Dto>) gwb001_1.get("CUSTOMERS");
            inDto.put("DISTRIBUTORS", _list1);

            list = gwat900Service.getGwakList(wapper.getPage(), inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("服务器开小差了");
        }
        return new SuccessResponseData(list);
    }

    /**
     * 保存客户信息
     *
     * @return
     */
    @RequestMapping(value = "updateGwak")
    public ResponseData updateGwak(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();

        GwakT gwak_tPO = new GwakT();
        GwaklT gwakl_tPO = new GwaklT();
        SysUser currentUser = wapper.getSysUser();
        try {
            gwak_tPO = CommonUtils.toJavaClass(inDto,GwakT.class);
            gwak_tPO.setGWAKENT(currentUser.getDBC());
            gwak_tPO.setGWAKMODU(currentUser.getID());

            gwakl_tPO =  CommonUtils.toJavaClass(inDto,GwaklT.class);
            gwakl_tPO.setGWAKLENT(currentUser.getDBC());
            gwakl_tPO.setGWAKL001(gwak_tPO.getGWAK001());
            gwat900Service.updateGwak(gwak_tPO, gwakl_tPO);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("服务器开小差了");
        }
        return new SuccessResponseData();
    }

    /**
     * 获取客户联系人列表
     *
     * @return
     */
    @RequestMapping(value = "getGwalList")
    public ResponseData getGwalList(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser user = wapper.getSysUser();
        List<Dto> list = new ArrayList<>();

        try {
            inDto.put("GWALENT", user.getDBC());
            inDto.put("LANG", LangUtils.getLang(user.getLANG()));
            inDto.put("GWALACTI", "Y");
            list = gwat900Service.getGwalList(wapper.getPage(),inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("服务器开小差了");
        }
        return new SuccessResponseData(list);
    }

    @RequestMapping(value = "getArea")
    public ResponseData getArea(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        inDto.put("LANG", "2");
        List<Dto> list = gwat900Service.getAreas(inDto);
        return new SuccessResponseData(list);
    }

    /**
     * 新建客户联系人时 检查客户常用地址是否存在
     *
     * @return
     */
    @RequestMapping(value = "checkType")
    public ResponseData checkType(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        Dto outDto = new Dto();
        SysUser user = wapper.getSysUser();
        try {
            inDto.put("GWALENT", user.getDBC());
            inDto.put("GWAKACTI", "Y");
            inDto.put("GWAL003", "Y");
            outDto.put("count", gwat900Service.checkType(inDto));
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("服务器开小差了");
        }
        return new SuccessResponseData(outDto);
    }

    /**
     * 保存客户信息
     *
     * @return
     */
    @RequestMapping(value = "saveGwal")
    public ResponseData saveGwal(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        GwakT gwak_tPO = new GwakT();
        GwalT gwal_tPO = new GwalT();
        SysUser currentUser = wapper.getSysUser();
        try {
            gwal_tPO._insert();
            gwal_tPO = CommonUtils.toJavaClass(inDto,GwalT.class);
            gwal_tPO.setGWALENT(currentUser.getDBC());
            gwal_tPO.setGWALACTI("Y");
            gwak_tPO.setGWAKENT(currentUser.getDBC());
            gwak_tPO.setGWAK001(inDto.getString("GWAL001"));
            gwak_tPO.setGWAK020(inDto.getString("GWAL010"));
            gwak_tPO.setGWAK021(inDto.getString("GWAL011"));
            gwak_tPO.setGWAK022(inDto.getString("GWAL012"));
            gwak_tPO.setGWAK023(inDto.getString("GWAL013"));
            gwak_tPO.setGWAK024(inDto.getString("GWAL014"));
            gwak_tPO.setGWAK025(inDto.getString("GWAL015"));
            gwak_tPO.setGWAK026(inDto.getString("GWAL016"));
            gwak_tPO.setGWAK027(inDto.getString("GWAL017"));
            gwak_tPO.setGWAK029(inDto.getString("GWAL019"));
            gwak_tPO.setGWAK030(inDto.getString("GWAL020"));
            gwak_tPO.setGWAK031(inDto.getString("GWAL021"));
            gwat900Service.saveGwal(gwak_tPO, gwal_tPO, inDto.getString("type"));
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("服务器开小差了");
        }
        return new SuccessResponseData();
    }

    /**
     * 删除客户联系人
     *
     * @return
     * @author wenchuan.li on 2018/08/28 11:07
     */
    @RequestMapping(value = "delGwal")
    public ResponseData delGwal(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser user = wapper.getSysUser();
        try {
            inDto.put("GWALENT", user.getDBC());
            gwat900Service.delGwal(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("服务器开小差了");
        }
        return new SuccessResponseData();
    }

    @RequestMapping(value = "getAddr")
    public ResponseData getAddr(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<GwalT> list = new ArrayList<>();
        try {
            QueryWrapper<GwalT> wrapper = new QueryWrapper<>();
            wrapper.select("GWAL002");
            wrapper.eq("GWAL001",inDto.getString("GWAK001"));
            wrapper.eq("GWAL003","Y");
            list = new GwalT().selectList(wrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("服务器开小差了");
        }
        return new SuccessResponseData(list);
    }

}
